<?php
/**
 *@author Connor <caokang@foxmail.com>
 *@todo
 *@link www.zeevin.com
 */
class Pic extends CActiveRecord{
	const PAGE_SIZE=12;

	public static function model($className=__CLASS__){
		return parent::model($className);
	}

	public function tableName(){
		return 'user_pics';
	}

	public function rules(){
		return array(
			array('picpath', 'file','types'=>'jpg, gif, png, jpeg, bmp','maxSize'=>1048576,'tooLarge'=>'图片不能超过1M', 'allowEmpty'=>false, 'on'=>'insert','message'=>'图片格式不正确','wrongType'=>'图片格式不正确只允许：jpg, gif, png, jpeg, bmp类型的图片'),
			array('title', 'required',  'on'=>'insert,update','message'=>'图片说明不能为空'),
			array('title', 'length', 'max'=>255, 'on'=>'insert,update','message'=>'图片说明文字过长'),
			);
	}

	public static function getList(){
		$condition = 'uid=:uid';
		$params['uid']=Yii::app()->user->id;
		$criteria=new CDbCriteria();
	

		$criteria->condition = $condition;
		$criteria->params = $params;
		$criteria->order = 'id DESC';
    	$count=Pic::model()->count($criteria);
    	$pages=new CPagination($count);

    	// results per page
   	 	$pages->pageSize=self::PAGE_SIZE;
    	$pages->applyLimit($criteria);
    	$rs['total_num'] = $count;
    	$rs['rows'] = Pic::model()->findAll($criteria);
    	$rs['pages'] = $pages;
    	return $rs;
	}

	public static function del($id){
		$count = Pic::model()->countBySql('select count(1) from user_pics where uid=? and id=?',array(Yii::app()->user->id,$id));
		if($count==1){return Pic::model()->deleteByPk($id);}
		else return false;
	}
}